﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QL_BenXe
{
    public partial class frm_QLBanVe : Form
    {
        public frm_QLBanVe()
        {
            InitializeComponent();
        }

        public void Get_TenKhachHang(String str)
        {
            txt_KhachHang.Text = str;
        }

        public void Get_MaKhachHang(String str)
        {
            txt_KhachHang.Tag = str;
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            TimKiemKhachHang frm = new TimKiemKhachHang();
            frm.Get_TenKhachHang = new TimKiemKhachHang.GetString(Get_TenKhachHang);
            frm.Get_MaKhachHang= new TimKiemKhachHang.GetString(Get_MaKhachHang);
            frm.Show();
        }

        private void frm_QLVe_Load(object sender, EventArgs e)
        {
            cmb_TuyenDuong.DisplayMember = "TenTuyenDuong";
            cmb_TuyenDuong.ValueMember = "MaTuyenDuong";
            cmb_TuyenDuong.DataSource = TuyenDuongBUS.Select_All();
        }

        private void cmb_Xe_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmb_GioXuatBen.DisplayMember = "GioXuatBen";
            cmb_GioXuatBen.DataSource = LichChayBUS.Select_GioXuatBen_ThemVe(cmb_TuyenDuong.SelectedValue.ToString(), cmb_Xe.SelectedValue.ToString());

            String GiaVe = GiaVeBUS.Select_GiaTien(cmb_Xe.SelectedValue.ToString(), cmb_TuyenDuong.SelectedValue.ToString());
            String tmp = "";
            while (GiaVe.Length > 3)
            {
                tmp = "." + GiaVe.Substring(GiaVe.Length - 3) + tmp;
                GiaVe = GiaVe.Substring(0, GiaVe.Length - 3);
            }
            GiaVe = GiaVe + tmp;
            lbl_TienVe.Text = GiaVe;
        }

        private void cmb_TuyenDuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmb_Xe.DisplayMember = "BienSo";
            cmb_Xe.ValueMember = "MaXe";
            cmb_Xe.DataSource = XeBUS.Select_Xe_by_MaTuyenDuong(cmb_TuyenDuong.SelectedValue.ToString(),DateTime.Today.ToShortDateString());
        }

        private void btn_NhapVe_Click(object sender, EventArgs e)
        {
            if (!txt_KhachHang.Text.Equals("") || checkBox1.Checked)
            {
                try
                {
                    Ve Ve = new Ve();
                    if (checkBox1.Checked)
                        Ve.MaKhachHang = "KHVL";
                    else
                        Ve.MaKhachHang = txt_KhachHang.Tag.ToString();
                    Ve.MaXe = cmb_Xe.SelectedValue.ToString();
                    Ve.MaGhe = cmb_Ghe.SelectedValue.ToString();
                    Ve.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
                    Ve.Ngay = DateTime.Now.ToString();
                    Ve.GioXuatBen = cmb_GioXuatBen.Text;
                    Ve.TinhTrang = "True";
                    bool b = VeBUS.Insert(Ve);
                    if (b == true)
                        MessageBox.Show("Thêm vé thành công !!!");

                    //dtg_Ve.DataSource = VeBUS.Select_All();

                    Ve ve = new Ve();
                    ve.MaXe = cmb_Xe.SelectedValue.ToString();
                    ve.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
                    ve.GioXuatBen = cmb_GioXuatBen.Text;
                    cmb_Ghe.DisplayMember = "TenGhe";
                    cmb_Ghe.ValueMember = "MaGhe";
                    cmb_Ghe.DataSource = GheBUS.Select_GheTrong(ve);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
                MessageBox.Show("Chưa nhập thông tin khách hàng !!!");
        }

        private void cmb_GioXuatBen_SelectedIndexChanged(object sender, EventArgs e)
        {
            Ve ve = new Ve();
            ve.MaXe = cmb_Xe.SelectedValue.ToString();
            ve.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
            ve.GioXuatBen = cmb_GioXuatBen.Text;
            cmb_Ghe.DisplayMember = "TenGhe";
            cmb_Ghe.ValueMember = "MaGhe";
            cmb_Ghe.DataSource = GheBUS.Select_GheTrong(ve);
        }
    }
}
